package com.guo.loading;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

/**
 * 加载中动效布局
 */
public class LoadingView extends LinearLayout {
    //运动小球
    private ShapeView shapeView;
    //地面倒影
    private ImageView imageViewReflection;
    //移动距离
    private int shiftDistance = 0;
    private AnimatorSet upAnimatorSet , downAnimatorSet;

    public LoadingView(Context context) {
        super(context);
    }

    public LoadingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public LoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(Context context , AttributeSet attrs){
        setOrientation(VERTICAL);
        LayoutInflater.from(context).inflate(R.layout.layout_loading_view,this , true);
        shapeView = findViewById(R.id.shape_view);
        imageViewReflection = findViewById(R.id.imageview_reflect);
        shiftDistance = dp2px(60);
    }

    /**
     * 在视图加载完毕时开始执行动效
     */
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if(getVisibility() == VISIBLE){
            startAnim();
        }
    }

    /**
     * 视图被移除时停止动效
     */
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        stopAnim();
    }

    private void startAnim(){
        toBottom();
    }

    private void stopAnim(){
        if(null != upAnimatorSet && upAnimatorSet.isRunning()){
            upAnimatorSet.cancel();
            upAnimatorSet.removeAllListeners();
        }
        if(null != downAnimatorSet && downAnimatorSet.isRunning()){
            downAnimatorSet.cancel();
            downAnimatorSet.removeAllListeners();
        }
    }

    /**
     * /减速上升
     */
    private void toTop(){
        shapeView.changeShape();
        ObjectAnimator upShiftAnimator = ObjectAnimator.ofFloat(shapeView,"translationY",
                shiftDistance,0);
        ObjectAnimator scaleAnimator = ObjectAnimator.ofFloat(imageViewReflection,"scaleX",1f,0.2f);
        ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(shapeView , "rotation",0,360);
        upAnimatorSet = new AnimatorSet();
        upAnimatorSet.playTogether(upShiftAnimator , scaleAnimator , rotateAnimator);
        upAnimatorSet.setDuration(1000);
        upAnimatorSet.setInterpolator(new DecelerateInterpolator());
        upAnimatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                toBottom();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        upAnimatorSet.start();
    }


    /**
     * 加速下降
     */
    private void toBottom(){
        ObjectAnimator bottomShiftAnimator = ObjectAnimator.ofFloat(shapeView,"translationY",
                0,shiftDistance);
        ObjectAnimator scaleAnimator = ObjectAnimator.ofFloat(imageViewReflection,"scaleX",0.2f,1f);
        ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(shapeView , "rotation",0,360);
        downAnimatorSet = new AnimatorSet();
        downAnimatorSet.playTogether(bottomShiftAnimator , scaleAnimator , rotateAnimator);
        downAnimatorSet.setDuration(1000);
        downAnimatorSet.setInterpolator(new AccelerateInterpolator());
        downAnimatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                toTop();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        downAnimatorSet.start();
    }

    /**
     * 将dp值转为像素
     * @param dp 输入dp类型尺寸
     * @return
     */
    private int dp2px(int dp){
       float dpi = getContext().getResources().getDisplayMetrics().density;
       return (int)(dp*dpi + 0.5f);
    }
}
